package org.dftproject.genesis.core.search;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dftproject.genesis.core.Activator;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;

public class SearchUtils {

	private static final Log log = LogFactory.getLog(SearchUtils.class);
	
	public static Set<ISearchProvider> getSearchProviders() {
		Set<ISearchProvider> providers = new HashSet<ISearchProvider>();
		
		IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(Activator.ID, Activator.SEARCH_PROVIDERS_EXTENSION_POINT);
		for (IExtension extension : extensionPoint.getExtensions()) {
			for (IConfigurationElement configElement : extension.getConfigurationElements()) {
				try {
					Object provider = configElement.createExecutableExtension("class");
					if (provider instanceof ISearchProvider)
						providers.add((ISearchProvider) provider);
				} catch (CoreException e) {
					log.error("Failed to create search provider", e);
				}
			}
		}
		
		return providers;
	}

}
